package cn.lg.soar.common.model;

import cn.lg.soar.common.enums.LogTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

import java.time.Duration;
import java.time.LocalDateTime;

/**
 * 日志数据
 * @author luguoxiang
 * @date 2022/4/23
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Getter
@Setter
public class LogDTO {

    @Schema(description = "日志类型")
    private LogTypeEnum type;

    @Schema(description = "执行开始时间（何时）")
    private LocalDateTime startTime;

    @Schema(description = "租户id(何人)")
    private Integer tenantId;

    @Schema(description = "用户类型(何人)")
    private Integer userType;

    @Schema(description = "用户id(何人)")
    private Long userId;

    @Schema(description = "客户端类型(何地)")
    private Integer clientType;

    @Schema(description = "客户端ip(何地)")
    private String clientIp;

    @Schema(description = "客户端设备信息(何地)")
    private String userAgent;

    @Schema(description = "标题(何事)")
    private String title;

    @Schema(description = "耗时（毫秒）")
    private Integer duration;

    @Schema(description = "链路id")
    private String traceId;

    @Schema(description = "哪个接口")
    private String path;

    @Schema(description = "日志内容(简要说明)")
    private String content;

    @Schema(description = "java方法")
    private String javaMethod;

    @Schema(description = "方法入参")
    private String args;

    @Schema(description = "方法出参")
    private String result;

    @Schema(description = "错误堆栈信息")
    private String errorStack;

    public void setDurationByStartTime(LocalDateTime startTime) {
        this.duration = (int) Duration.between(startTime, LocalDateTime.now()).toMillis();
    }

    public void setStartTimeAndDuration(LocalDateTime startTime) {
        this.startTime = startTime;
        this.duration = (int) Duration.between(startTime, LocalDateTime.now()).toMillis();
    }

}
